﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:UnitBase
    /// </summary>
    public partial class UnitBase
    {
        public UnitBase()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("UnitBaseID", "UnitBase");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int UnitBaseID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from UnitBase");
            strSql.Append(" where UnitBaseID=@UnitBaseID");
            var parameters = new SqlParameter[] { new SqlParameter("@UnitBaseID", SqlDbType.Int, 4)
            };
            parameters[0].Value = UnitBaseID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.UnitBase model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into UnitBase(");
            strSql.Append("BaseCode,BaseName,IsStop,Deleted)");
            strSql.Append(" values (");
            strSql.Append("@BaseCode,@BaseName,@IsStop,@Deleted)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@BaseCode", SqlDbType.VarChar, 20),
                    new SqlParameter("@BaseName", SqlDbType.VarChar, 20),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1) };
            parameters[0].Value = model.BaseCode;
            parameters[1].Value = model.BaseName;
            parameters[2].Value = model.IsStop;
            parameters[3].Value = model.Deleted;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.UnitBase model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update UnitBase set ");
            strSql.Append("BaseCode=@BaseCode,");
            strSql.Append("BaseName=@BaseName,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("Deleted=@Deleted");
            strSql.Append(" where UnitBaseID=@UnitBaseID");
            var parameters = new SqlParameter[] { new SqlParameter("@BaseCode", SqlDbType.VarChar, 20),
                    new SqlParameter("@BaseName", SqlDbType.VarChar, 20),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@UnitBaseID", SqlDbType.Int, 4) };
            parameters[0].Value = model.BaseCode;
            parameters[1].Value = model.BaseName;
            parameters[2].Value = model.IsStop;
            parameters[3].Value = model.Deleted;
            parameters[4].Value = model.UnitBaseID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int UnitBaseID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from UnitBase ");
            strSql.Append(" where UnitBaseID=@UnitBaseID");
            var parameters = new SqlParameter[] { new SqlParameter("@UnitBaseID", SqlDbType.Int, 4)
            };
            parameters[0].Value = UnitBaseID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string UnitBaseIDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from UnitBase ");
            strSql.Append(" where UnitBaseID in (" + UnitBaseIDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.UnitBase GetModel(int UnitBaseID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 UnitBaseID,BaseCode,BaseName,IsStop,Deleted from UnitBase ");
            strSql.Append(" where UnitBaseID=@UnitBaseID");
            var parameters = new SqlParameter[] { new SqlParameter("@UnitBaseID", SqlDbType.Int, 4)
            };
            parameters[0].Value = UnitBaseID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.UnitBase DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.UnitBase();
            if (row != null)
            {
                if (row["UnitBaseID"] != null && row["UnitBaseID"].ToString() != string.Empty)
                {
                    model.UnitBaseID = int.Parse(row["UnitBaseID"].ToString());
                }
                if (row["BaseCode"] != null)
                {
                    model.BaseCode = row["BaseCode"].ToString();
                }
                if (row["BaseName"] != null)
                {
                    model.BaseName = row["BaseName"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    model.IsStop = int.Parse(row["IsStop"].ToString());
                }
                if (row["Deleted"] != null && row["Deleted"].ToString() != string.Empty)
                {
                    if ((row["Deleted"].ToString() == "1") || (row["Deleted"].ToString().ToLower() == "true"))
                    {
                        model.Deleted = true;
                    }
                    else
                    {
                        model.Deleted = false;
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select UnitBaseID,BaseCode,BaseName,IsStop,Deleted ");
            strSql.Append(" FROM UnitBase ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" UnitBaseID,BaseCode,BaseName,IsStop,Deleted ");
            strSql.Append(" FROM UnitBase ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM UnitBase ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.UnitBaseID desc");
            }
            strSql.Append(")AS Row, T.*  from UnitBase T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }



        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public DataSet GetListExpand(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select UnitBaseID,BaseCode,BaseName,BaseCode+'-'+BaseName [No-Name],IsStop,Deleted ");
            strSql.Append(" FROM UnitBase ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public DataSet GetListForUIExpand(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select UnitBaseID,BaseCode,BaseName,IsStop,Deleted ");
            strSql.Append(" FROM UnitBase ");
            strSql.Append(" where Deleted=0 ");

            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表 扩展
        /// </summary>
        public int AddExpand(DMZS.Model.UnitBase model)
        {
            var strSql = @"
declare @codeCount int
select @codeCount=count(1) from UnitBase where BaseCode=@BaseCode
if @codeCount>0 
begin
    select -1001
    return
end

insert into UnitBase(BaseCode,BaseName,IsStop,Deleted) 
values(@BaseCode,@BaseName,@IsStop,@Deleted)
select @@IDENTITY";
            var parameters = new SqlParameter[] { new SqlParameter("@BaseCode", SqlDbType.VarChar, 50),
                    new SqlParameter("@BaseName", SqlDbType.VarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1) };
            parameters[0].Value = model.BaseCode;
            parameters[1].Value = model.BaseName;
            parameters[2].Value = model.IsStop;
            parameters[3].Value = model.Deleted;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// 返回-1001:编号重复
        /// </summary>
        public int UpdateExpand(DMZS.Model.UnitBase model)
        {
            var strSql = @"
declare @codeCount int
select @codeCount=count(1) from UnitBase where BaseCode=@BaseCode and UnitBaseID<>@UnitBaseID
if @codeCount>0 
begin
    select -1001
    return
end

update UnitBase set
    BaseCode=@BaseCode,
    BaseName=@BaseName,
    IsStop=@IsStop,
    Deleted=@Deleted
where UnitBaseID=@UnitBaseID
select @UnitBaseID";
            var parameters = new SqlParameter[] { new SqlParameter("@BaseCode", SqlDbType.VarChar, 50),
                    new SqlParameter("@BaseName", SqlDbType.VarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@Deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@UnitBaseID", SqlDbType.Int, 4) };
            parameters[0].Value = model.BaseCode;
            parameters[1].Value = model.BaseName;
            parameters[2].Value = model.IsStop;
            parameters[3].Value = model.Deleted;
            parameters[4].Value = model.UnitBaseID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一行记录 扩展
        /// </summary>
        /// <param name="UnitBaseID"></param>
        public int DeleteExpand(int UnitBaseID)
        {
            var strSql = @"
update UnitBase set
    Deleted=1
where UnitBaseID=@UnitBaseID
select @UnitBaseID";
            var parameters = new SqlParameter[] { new SqlParameter("@UnitBaseID", SqlDbType.Int, 4) };
            parameters[0].Value = UnitBaseID;
            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 第一条记录
        /// </summary>
        /// <returns></returns>
        public DataRow GetFirst()
        {
            var strSql = @"
select top 1 UnitBaseID,BaseCode,BaseName,IsStop,Deleted
from UnitBase u
";
            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        /// <summary>
        /// 最后一条记录
        /// </summary>
        /// <returns></returns>
        public DataRow GetLast()
        {
            var strSql = @"
select top 1 UnitBaseID,BaseCode,BaseName,IsStop,Deleted
from UnitBase u
order by UnitBaseID desc
";
            var ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        /// <summary>
        /// 上一条记录
        /// </summary>
        /// <param name="UnitBaseID"></param>
        /// <returns></returns>
        public DataRow GetPrevious(int UnitBaseID)
        {
            var strSql = @"
select top 1 UnitBaseID,BaseCode,BaseName,IsStop,Deleted
from UnitBase u
where u.Deleted=0 and u.UnitBaseID<@UnitBaseID
order by UnitBaseID desc
";

            var paramters = new SqlParameter[] { new SqlParameter("@UnitBaseID", SqlDbType.Int, 4)
            };
            paramters[0].Value = UnitBaseID;
            var ds = DbHelperSQL.Query(strSql.ToString(), paramters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }
        /// <summary>
        /// 下一条记录
        /// </summary>
        /// <param name="UnitBaseID"></param>
        /// <returns></returns>
        public DataRow GetNext(int UnitBaseID)
        {
            var strSql = @"
select top 1 UnitBaseID,BaseCode,BaseName,IsStop,Deleted
from UnitBase u
where u.Deleted=0 and u.UnitBaseID>@UnitBaseID
order by UnitBaseID 
";

            var paramters = new SqlParameter[] { new SqlParameter("@UnitBaseID", SqlDbType.Int, 4)
            };
            paramters[0].Value = UnitBaseID;

            var ds = DbHelperSQL.Query(strSql.ToString(), paramters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        public DataRow GetRecordByCode(string Code)
        {
            var strSql = "select top 1 UnitBaseID,BaseCode,BaseName,IsStop,Deleted from UnitBase where BaseCode=@BaseCode ";//and Deleted=0
            var paramters = new SqlParameter[] { new SqlParameter("@BaseCode", SqlDbType.VarChar, 20)
            };
            paramters[0].Value = Code;

            var ds = DbHelperSQL.Query(strSql.ToString(), paramters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            return ds.Tables[0].Rows[0];
        }

        public DataSet GetListForDialog(string Code, string Name)
        {
            var strSql = new StringBuilder();
            strSql.Append("select UnitBaseID,BaseCode,BaseName,IsStop,Deleted ");
            strSql.Append(" FROM UnitBase ");
            strSql.Append(" where Deleted=0 and BaseCode like '%'+@Code+'%' and BaseName like '%'+@Name+'%'  ");

            var parameters = new SqlParameter[] { 
                new SqlParameter("@Code", SqlDbType.NVarChar, 50),
                new SqlParameter("@Name", SqlDbType.NVarChar, 100)};
            parameters[0].Value = Code;
            parameters[1].Value = Name;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }
    }
}

